import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:求该数组中的前3个最小元素
 * User: 东莞呵呵
 * Date:2022-06-18
 * Time:15:44
 */
public class TestDemo {
    public static void main(String[] args) {
        int[] arr={34,23,67,3,546,21,12};
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(3, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        int[] ret=TopK(arr,priorityQueue);
        System.out.println(Arrays.toString(ret));
    }

    public static int[] TopK(int[] arr,PriorityQueue<Integer> priorityQueue){
        for (int i = 0; i < arr.length; i++) {
            if(i<3){
                priorityQueue.offer(arr[i]);
            }else{
                if(priorityQueue.peek()>arr[i]){
                    priorityQueue.poll();
                    priorityQueue.offer(arr[i]);
                }
            }
        }
        int[] ret=new int[3];
        for (int i = 0; i < 3; i++) {
            ret[i]=priorityQueue.poll();
        }
        return ret;
    }
}
